package com.lanou.controller;

import com.lanou.entity.User;
import com.lanou.service.UserService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author lizhenjiang
 * @Date 2025年08月12日 17:39
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public R addUser(@RequestBody User user) {
        return R.ok(userService.addUser(user));
    }

    @PutMapping
    public R updateUser(@RequestBody User user) {
        return R.ok(userService.updateUser(user));
    }

    @DeleteMapping("{id}")
    public R deleteUser(@PathVariable Integer id) {
        return R.ok(userService.deleteUser(id));
    }

    @GetMapping("{id}")
    public R getUserById(@PathVariable Integer id) {
        return R.ok(userService.findUserById(id));
    }

    @GetMapping
    public R login(@RequestParam String account, @RequestParam String password) {
        User user = userService.login(account, password);
        if (user != null) {
            return R.ok(user);
        }else {
            return R.fail("账号或密码有误");
        }
    }

    @GetMapping("/checkPhone")
    public R checkPhone(String phone) {
        return R.ok(userService.findUserPhone(phone));
    }
}
